package com.example.shushiyunsst.fragment.main.transaction.child.content

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.library_common.album.view.grid_view.XGridLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.LazyFragment
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.bean.TransIndexWayBean
import com.example.shushiyunsst.fragment.main.transaction.bean.TransactionUIInvokeListener
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.KlineChartPortraitFragment
import com.example.shushiyunsst.fragment.main.transaction.child.TRANSFER_DECIMAL_FORMAT
import com.example.shushiyunsst.fragment.main.transaction.child.transfer_index_data
import com.example.shushiyunsst.widget.TransactionBtn
import com.example.shushiyunsst.widget.TransactionBtn.*
import com.example.wallet.global.AppGlobals
import java.text.DecimalFormat

/**
 * 当前交易趋势
 * */
class TransactionCurrentTendencyFragment : LazyFragment() {
    companion object {
        fun newInstance(): TransactionCurrentTendencyFragment {
            val args = Bundle()
            val fragment = TransactionCurrentTendencyFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var mSellXGridLayout: XGridLayout

    private lateinit var mBuyXGridLayout: XGridLayout

    private lateinit var priceTv: TextView

    private lateinit var mSellLayout: View

    private lateinit var mBuyLayout: View

    private lateinit var mTransactionBtn: TransactionBtn

    private var mSellXGridAdapter: CommAdapter<TransIndexWayBean>? = null

    private var mBuyXGridAdapter: CommAdapter<TransIndexWayBean>? = null

    private val mBuyList = mutableListOf<TransIndexWayBean>()

    private val mSellList = mutableListOf<TransIndexWayBean>()

    private val maxSize = 5

    private lateinit var avgPriceTv: TextView

    private lateinit var avgPriceCny: TextView

    private val cnyStr = AppGlobals.getApplication().getString(R.string.wallet_cny)

    private lateinit var mKlineBtn:ImageButton

    private val df = DecimalFormat(TRANSFER_DECIMAL_FORMAT)

    override fun getLayoutId() = R.layout.transaction_current_tendency_fragment

    private var mTransactionSettingInvoker: TransactionSettingInvoker? = null

    fun setTransactionSettingInvoker(mTransactionSettingInvoker: TransactionSettingInvoker?) {
        this.mTransactionSettingInvoker = mTransactionSettingInvoker
    }

    private var transactionUIInvokeListener: TransactionUIInvokeListener? = null

    fun setTransactionUIInvokeListener(listener: TransactionUIInvokeListener?): BaseFragmentEx {
        this.transactionUIInvokeListener = listener
        return this
    }

    override fun lazyInit() {

    }


    override fun initView(view: View) {
        avgPriceTv = view.findViewById(R.id.avgPriceTv)

        avgPriceCny = view.findViewById(R.id.avgPriceCny)


        priceTv = view.findViewById(R.id.priceTv)
        mSellLayout = view.findViewById(R.id.mSellLayout)
        mBuyLayout = view.findViewById(R.id.mBuyLayout)
        mTransactionBtn = view.findViewById(R.id.mTransactionBtn)
        mSellXGridLayout = view.findViewById(R.id.mSellXGridLayout)
        mBuyXGridLayout = view.findViewById(R.id.mBuyXGridLayout)
        mKlineBtn= view.findViewById(R.id.mKlineBtn)
        mSellXGridAdapter = object : CommAdapter<TransIndexWayBean>
            (_mActivity, R.layout.transaction_current_tendency_item) {
            override fun convert(
                holder: XQuickViewHolder?,
                item: TransIndexWayBean?,
                position: Int
            ) {
                val textColor = ContextCompat.getColor(_mActivity, R.color.image_color_red)
                val priceTv=df.format(item?.txPrice)
                val countTv=df.format(item?.txNums)
                holder?.setTextColor(R.id.priceTv, textColor)
                    ?.setTextColor(R.id.countTv, textColor)
                    ?.setText(R.id.priceTv, priceTv)
                    ?.setText(R.id.countTv, countTv)
                    ?.setOnClickListener {
                        mTransactionSettingInvoker?.selectCurrent(item)
                    }
            }

        }

        mSellXGridLayout.setAdapter(mSellXGridAdapter, lifecycle)


        mBuyXGridAdapter = object : CommAdapter<TransIndexWayBean>
            (_mActivity, R.layout.transaction_current_tendency_item) {
            override fun convert(
                holder: XQuickViewHolder?,
                item: TransIndexWayBean?,
                position: Int
            ) {
                val textColor = ContextCompat.getColor(_mActivity, R.color.PrimaryColor)
                val priceTv=df.format(item?.txPrice)
                val countTv=df.format(item?.txNums)
                holder?.setTextColor(R.id.priceTv, textColor)
                    ?.setTextColor(R.id.countTv, textColor)
                    ?.setText(R.id.priceTv, priceTv)
                    ?.setText(R.id.countTv, countTv)
                    ?.setOnClickListener {
                        mTransactionSettingInvoker?.selectCurrent(item)
                    }
            }

        }
        mBuyXGridLayout.setAdapter(mBuyXGridAdapter, lifecycle)

        mTransactionBtn.setOnClickListener {
            changList()
        }

        mKlineBtn.setOnClickListener {
            val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
            start(KlineChartPortraitFragment.newInstance(transIndexNode))
        }
    }


    @SuppressLint("SetTextI18n")
    override fun handMessage(msg: MessageWrap?) {
//        super.handMessage(msg)
//        mList.clear()

        when (msg?.message) {
            transfer_index_data -> {

                val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
                val bean = transIndexNode?.bean
                val newBuyList = bean?.buyList

                val avgPrice=df.format(transIndexNode?.bean?.avgPrice)
                avgPriceTv.text = avgPrice

                avgPriceCny.text = "≈ $avgPrice $cnyStr  "

                priceTv.text = "价格 ${transIndexNode?.iconType?.tokenName}"

                mBuyList.clear()
                mBuyList.addAll(newBuyList ?: emptyList())

                val newSellList = bean?.sellList
                mSellList.clear()
                mSellList.addAll(newSellList ?: emptyList())

                changList()
            }
        }
    }

    private fun changList() {
        mSellLayout.visibility = GONE
        mBuyLayout.visibility = GONE

        mBuyXGridAdapter?.clear()
        mSellXGridAdapter?.clear()
//        mList.clear()
        when (mTransactionBtn.status) {
            AN_JIU_STATUS -> {
                mSellLayout.visibility = VISIBLE
                mBuyLayout.visibility = VISIBLE
                //买卖
                val buyLs = mutableListOf<TransIndexWayBean>()
                val buySize = if (mBuyList.size < maxSize) mBuyList.size else maxSize
                for (i in 0 until buySize) {
                    val e = mBuyList[i]
                    e.txType = 1
                    buyLs.add(e)
                }
                mBuyXGridAdapter?.addAll(buyLs)

                val sellLs = mutableListOf<TransIndexWayBean>()
                val sellSize = if (mSellList.size < maxSize) mSellList.size else maxSize
                for (i in 0 until sellSize) {
                    val e = mSellList[i]
                    e.txType = 2
                    sellLs.add(e)
                }
                mSellXGridAdapter?.addAll(sellLs)

            }
            LV_SE_STATUS -> {
                // 买

                mSellLayout.visibility = INVISIBLE
                mBuyLayout.visibility = VISIBLE


                val buyLs = mutableListOf<TransIndexWayBean>()

                for (e in mBuyList) {

                    e.txType = 1
                    buyLs.add(e)
                }
                mBuyXGridAdapter?.addAll(buyLs)
            }
            HONG_SE_STATUS -> {
                // 卖
                mSellLayout.visibility = VISIBLE
                mBuyLayout.visibility = INVISIBLE

                val sellLs = mutableListOf<TransIndexWayBean>()

                for (e in mSellList) {
                    e.txType = 2
                    sellLs.add(e)
                }
                mSellXGridAdapter?.addAll(sellLs)
            }
        }

        mSellXGridAdapter?.notifyDataSetChanged()
        mBuyXGridAdapter?.notifyDataSetChanged()
    }


}